package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.ClassifyShopEntity;
import com.zw.yzk.kabu.data.network.task.ClassifyShopTask;
import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.network.entity.GoodsClassifyEntity;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.GoodsClassifyMapper;
import com.zw.yzk.kabu.view.view.GoodsClassifyView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class GoodsClassifyPresenter implements Presenter {

    private UseCase getGoodsClassify;
    private UseCase getClassifyShop;
    private AppCompatActivity activity;
    private GoodsClassifyMapper mapper;
    private GoodsClassifyView classifyView;

    @Inject
    public GoodsClassifyPresenter(@Named("goodsClassifyUseCase") UseCase getGoodsClassify,
                                 @Named("classifyShopUseCase") UseCase getClassifyShop,
                                  GoodsClassifyMapper mapper,
                                  AppCompatActivity activity) {
        this.getGoodsClassify = getGoodsClassify;
        this.getClassifyShop = getClassifyShop;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(GoodsClassifyView view) {
        this.classifyView = view;
    }

    public void getGoodsClassify() {
        getGoodsClassify.execute(new GoodsClassifySubscriber(activity), new Task());
    }

    public void getClassifyShop(int start, int type) {
        getClassifyShop.execute(new ClassifyShopSubscriber(activity), new ClassifyShopTask(type, start));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getGoodsClassify.unSubscribe();
        getClassifyShop.unSubscribe();
    }

    private class GoodsClassifySubscriber extends BaseSubscriber<GoodsClassifyEntity> {

        public GoodsClassifySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(GoodsClassifyEntity goodsClassifyEntity) {
            super.onNext(goodsClassifyEntity);
            classifyView.getGoodsClassifySuccess(mapper.transfer(goodsClassifyEntity));
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_goods_classify_failed);
        }
    }

    private class ClassifyShopSubscriber extends BaseSubscriber<ClassifyShopEntity> {

        public ClassifyShopSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_goods_classify_failed);
        }

        @Override
        public void onNext(ClassifyShopEntity entity) {
            super.onNext(entity);
            classifyView.getClassifyShopSuccess(mapper.transferShop(entity), mapper.transferClassify(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            classifyView.getClassifyShopFailed();
        }
    }
}
